<template>
	<view class="content">
		<view class="list" v-for="(item, index) in address_list" :key="index" @click="selectaddres(item)">
			<view class="heard">
				<view class="name">
					<view class="title" v-if="item.is_default == 1">默认</view>
					{{ item.real_name }}
				</view>
				<view class="phone">{{ item.phone }}</view>
			</view>
			<view class="address">{{ item.province + item.city + item.district }}</view>
			<view class="fengexian"></view>
			<view class="dibu">
				<label v-if="item.is_default == 1" class="radio">
					<!-- <radio style="transform: scale(0.8);" value="r1" checked="true" /> -->
					</label>
				<label v-else class="radio">
					<!-- <radio style="transform: scale(0.8);" value="r1" @click="saveAddressBook(item.id)" /> -->
					</label>
				<view class="title"></view> 

				<view class="deit" @click.stop="deit(item)">
					<image src="http://tcyh-1313348853.cos.ap-shanghai.myqcloud.com/tcyh/2022-09-03/tcyh4980141662195270940.png" mode=""></image>
					编辑
				</view>
				<view class="detele" @click.stop="delAddress(item)">
					<image src="http://tcyh-1313348853.cos.ap-shanghai.myqcloud.com/tcyh/2022-09-03/tcyh8204541662195225781.png" mode=""></image>
					删除
				</view>
			</view>
		</view>
		<view class="zanwu">
			<view class="fenge"></view>
			<view class="wu">没有更多了</view>
			<view class="fenge"></view>
		</view>

		<view class="fooder" @click="deit"><view class="">添加地址</view></view>
	</view>
</template>

<script>
import { addressList, deleteAddress, setAddress } from '@/api/api.js';
export default {
	components: {},
	data() {
		return {
			isMr: false, //是否设置默认
			address_list: [], //地址列表
			from: {},
			orderId:'',
			liwai:''
		};
	},
	onLoad(options) {
		console.log(options)
		this.orderId = options.orderId
		this.liwai = options.liwai
		this.addressBookList();
	},
	onShow() {
		this.addressBookList();
	},
	methods: {
		isfh() {
			uni.navigateBack({
				delta: 1
			});
		},
		// 地址列表
		addressBookList() {
			addressList().then(res => {
				this.address_list = res.data.data;
			});
		},
		//编辑地址设为默认
		saveAddressBook(id) {
			let data = `addressId=${id}`;
			setAddress(data).then(res => {
				if (res.data.code == '200') {
					uni.showToast({
						title: '修改成功',
						duration: 2000
					});
					this.addressBookList();
				} else {
					uni.showToast({
						title: res.data.msg,
						duration: 2000,
						icon:'error'
					});
				}
			});
		},
		//跳转到编辑地址
		deit(item) {
			if (item.id == undefined) {
				uni.navigateTo({
					url: '/myIndex/addAddress/addAddress?id=' + 0
				});
			} else {
				item.adress = item.province + item.city + item.district;
				let data = JSON.stringify(item);
				uni.navigateTo({
					url: '/myIndex/addAddress/addAddress?data=' + data
				});
			}
		},
		//删除地址
		delAddress(item) {
			let that = this;
			let data = `addressId=${item.id}`;

			uni.showModal({
				title: '提示',
				content: '确定要删除该地址',
				success: function(res) {
					if (res.confirm) {
						deleteAddress(data).then(res => {
							if (res.data.code == '200') {
								uni.showToast({
									title: '删除成功',
									duration: 2000
								});
								that.addressBookList();
							} else {
								uni.showToast({
									title: '删除失败',
									duration: 2000,
									icon:'error'
								});
							}
						});
					} else if (res.cancel) {
						console.log('用户点击取消');
					}
				}
			});
		},
		selectaddres(item){
			// 获取所有页面栈实例列表
			let pages = getCurrentPages();  
			// 当前页页面实例
			let nowPage = pages[ pages.length - 1]; 
			// 上一页页面实例
			let prevPage = pages[ pages.length - 2 ];
			// 修改上一页data里面的times参数值为100

			if(this.orderId == undefined){
				return
			}
			if(this.liwai == undefined){
				let obj = JSON.stringify(item)
				prevPage.$vm.addObj = obj;
				//uni.navigateTo跳转的返回，默认1为返回上一级
				uni.navigateBack({  
				    delta: 1
				});
			}else {
				let obj = JSON.stringify(item)
				prevPage.$vm.addObj = obj;
				//uni.navigateTo跳转的返回，默认1为返回上一级
				uni.navigateBack({  
				    delta: 1
				});
			}
			
		}
	}
};
</script>

<style lang="scss">
.content {
	width: 100vw;
	min-height: 100vh;
	overflow: hidden;
	position: relative;
	background-color: rgb(240, 240, 240);

	.list {
		width: 100%;
		height: 200rpx;
		background-color: #fff;
		margin-top: 20rpx;
		overflow: hidden;

		.heard {
			width: 700rpx;
			height: 40rpx;
			margin: 20rpx auto 0;
			display: flex;
			justify-content: space-between;

			.name {
				max-width: 400rpx;
				height: 40rpx;
				line-height: 40rpx;
				display: flex;
				justify-content: space-between;
				align-items: center;
				color: rgb(64, 64, 64);
				font-size: 32rpx;

				.title {
					width: 70rpx;
					text-align: center;
					height: 35rpx;
					line-height: 35rpx;
					border-radius: 10rpx;
					font-size: 20rpx;
					margin-right:10rpx;
					background-color: #3db963;
					color: #fff;
				}
			}

			.phone {
				width: 200rpx;
				height: 40rpx;
				line-height: 40rpx;
				text-align: right;
				color: #000;
				font-size: 32rpx;
			}
		}

		.address {
			width: 700rpx;
			height: 32rpx;
			font-size: 24rpx;
			color: rgb(169, 169, 169);
			font-size: 24rpx;
			line-height: 32rpx;
			margin: 15rpx auto 0;
		}

		.fengexian {
			// width: 100%;
			// height: 1rpx;
			// background-color: rgb(228, 228, 228);
			margin-top: 20rpx;
		}

		.dibu {
			width: 700rpx;
			height: 70rpx;
			margin: 0 auto;
			display: flex;
			justify-content: start;
			align-items: center;
			.radio {
				display: block;
				width: 30rpx;
				height: 30rpx;
				margin-top: -20rpx;
			}

			.ismr {
				width: 30rpx;
				height: 30rpx;
				border: 1rpx solid rgb(175, 175, 175);
				border-radius: 50%;
			}

			.ismr2 {
				width: 30rpx;
				height: 30rpx;
				border: 1rpx solid rgb(175, 175, 175);
				border-radius: 50%;
				background-color: #000;
			}

			.title {
				width: 100rpx;
				height: 30rpx;
				line-height: 30rpx;
				font-size: 24rpx;
				color: rgb(64, 64, 64);
				margin-left: 30rpx;
			}

			.deit {
				width: 90rpx;
				height: 30rpx;
				line-height: 30rpx;
				font-size: 24rpx;
				color: rgb(64, 64, 64);
				display: flex;
				align-items: center;
				justify-content: space-between;
				margin-left: 280rpx;

				image {
					width: 26rpx;
					height: 26rpx;
				}
			}

			.detele {
				width: 90rpx;
				height: 30rpx;
				line-height: 30rpx;
				font-size: 24rpx;
				color: rgb(64, 64, 64);
				display: flex;
				align-items: center;
				justify-content: space-between;
				margin-left: 70rpx;

				image {
					width: 26rpx;
					height: 26rpx;
				}
			}
		}
	}
}

.fooder {
	font-size: 30rpx;
	position: fixed;
	bottom: 40rpx;
	left: 50%;
	transform: translateX(-50%);
	display: flex;
	justify-content: center;

	> view {
		padding: 20rpx 30rpx;
		background-color: #3db963;
		color: #fff;
		border-radius: 15rpx;
		width: 400rpx;
		text-align: center;
	}
}
.zanwu {
	display: flex;
	align-items: center;
	justify-content: center;
	margin-top: 30rpx;
	font-size: 24rpx;
	color: #bcbcbc;
	.fenge {
		height: 2rpx;
		width: 50rpx;
		background-color: #bcbcbc;
	}
	.wu {
		margin: 0 15rpx;
	}
}
</style>
